from app import mail
from flask_mail import Message
from flask import current_app, render_template
from threading import Thread


def send_async_mail(app, msg):
    with app.app_context():
        try:
            mail.send(msg)
        except Exception as e:
            raise e


def send_mail(to, subject, template, **kwargs):
    """
    发送邮件
    :param to:
    :param subject:
    :param template:
    :param kwargs:
    :return:
    """
    msg = Message(
        '[鱼书] ' + subject,
        sender=current_app.config['MAIL_USERNAME'],
        recipients=[to]
    )
    msg.html = render_template(template, **kwargs)
    # 开启子线程,异步执行发送
    app = current_app._get_current_object()
    thr = Thread(target=send_async_mail, args=[app, msg])
    thr.start()
